/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.up.kafka.productor;

import org.springframework.kafka.support.SendResult;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author: baiMl
 * @title: KafkaProducer
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2022/4/24 13:59
 */
public interface KafkaProducer<V> {
    /**
     * 发送一次消息，发送不用等待ack，性能最好，但是可能会丢失消息
     *
     * @param record 发送对象
     */
    void sendOnce(MessageRecord<V> record);

    /**
     * 同步发送，等待发送结果
     *
     * @param record 发送的对象
     * @return 返回同步等待结果对象
     * @throws ExecutionException   发送执行异常
     * @throws InterruptedException 同步等待中断异常
     */
    SendResult<String, V> sendSync(MessageRecord<V> record) throws ExecutionException, InterruptedException;

    /**
     * 阻塞等待结果
     *
     * @param record   发送的对象
     * @param time     等待的时间
     * @param timeUnit 时间单位
     * @return 返回同步等待结果对象
     * @throws InterruptedException 同步等待中断异常
     * @throws ExecutionException   发送执行异常
     * @throws TimeoutException     超时等待异常
     */
    SendResult<String, V> sendSync(MessageRecord<V> record, long time, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException;

    /**
     * 添加回调
     *
     * @param record   发送的对象
     * @param callBack 发送成功回调
     */
    void sendAndCallBack(MessageRecord<V> record, MsgCallBack<V> callBack);


}
